<template>
<div>
  <!--  面包屑导航-->
  <el-breadcrumb separator-class="el-icon-arrow-right">
    <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>商品管理</el-breadcrumb-item>
    <el-breadcrumb-item>商品分类</el-breadcrumb-item>
  </el-breadcrumb>
<!--  卡片视图区域-->
  <el-card>
<!--    按钮-->
    <el-row :gutter="30" class="margin-button">
      <el-col :span="6">
        <div style="margin-top: 15px;">
          <el-button type="primary" @click="showCateDialogVisible">添加商品分类</el-button>
        </div>
      </el-col>
    </el-row>
<!--表格区域-->
    <tree-table
      class="treetable"
      :data="catelist"
      :columns="columns"
      :selection-type="false"
      :expand-type="false"
      show-index
      index-text="#"
      border
    >
<!--      是否有效-->
      <template slot="isOk" slot-scope="scope">
        <i class="el-icon-success" style="color: #0077aa" v-if="scope.row.cat_deleted === false"></i>
        <i class="el-icon-error" style="color: #9a6e3a" v-else></i>
      </template>
<!--      排序-->
      <template slot="order" slot-scope="scope">
        <el-tag size="mini" v-if="scope.row.cat_level === 0">一级</el-tag>
        <el-tag size="mini" type="success" v-else-if="scope.row.cat_level === 1">二级</el-tag>
        <el-tag size="mini" type="warning" v-else>三级</el-tag>
      </template>
<!--      操作 slot-scope="scope"-->
      <template slot="opt">
        <el-button size="mini" type="primary" icon="el-icon-edit">编辑</el-button>
        <el-button size="mini" type="danger" icon="el-icon-delete">删除</el-button>
      </template>
    </tree-table>
<!--    分页-->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pagenum"
      :page-sizes="[3, 5, 10, 20]"
      :page-size="queryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
<!--    添加分类对话框-->
    <el-dialog
      @close="addCateDialogClosed"
      title="提示"
      :visible.sync="cateDialogVisible"
      width="70%">
      <el-form :model="addCateForm" :rules="addCateFormRules" ref="addCateFormRef" label-width="100px">
        <el-form-item label="分类名称" prop="cat_name">
          <el-input v-model="addCateForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级分类">
<!--          props显示配置对象
              options数据源
-->
          <el-cascader
            expand-trigger="hover"
            :options="parentCateList"
            :props="cascaderProps"
            v-model="selectKeys"
            @change="cascaderHandleChange" clearable change-on-select></el-cascader>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
    <el-button @click="cateDialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="addCate">确 定</el-button>
  </span>
    </el-dialog>
  </el-card>
</div>
</template>

<script>
export default {
  name: 'categories',
  data () {
    return {
      queryInfo: {
        type: 3,
        // 当前页数
        pagenum: 1,
        pagesize: 5
      },
      // 商品分类数据
      catelist: [],
      // 总数据条数
      total: 0,
      // 为table指定列的定义
      columns: [
        {
          label: '分类名称',
          prop: 'cat_name'
        },
        {
          label: '是否有效',
          type: 'template',
          template: 'isOk'
        },
        {
          label: '排序',
          type: 'template',
          template: 'order'
        },
        {
          label: '操作',
          type: 'template',
          template: 'opt'
        }
      ],
      //  添加分类对话框显示与隐藏
      cateDialogVisible: false,
      // 添加分类对话框数据
      addCateForm: {
        // 父级分类id
        cat_pid: 0,
        // 分类名称
        cat_name: '',
        // 分类层级 默认一级分类
        cat_level: 0
      },
      // 表单验证规则
      addCateFormRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      parentCateList: [],
      // 指定级联选择器配置显示信息
      cascaderProps: {
        value: 'cat_id',
        label: 'cat_name',
        children: 'children'
      },
      // 选中的id
      selectKeys: []
    }
  },
  created () {
    this.getCateList()
  },
  methods: {
    async getCateList () {
      const { data } = await this.$axios.get('categories', { params: this.queryInfo })
      if (data.meta.status !== 200) return this.$message.error(data.meta.msg)
      // 获取成功
      this.catelist = data.data.result
      this.total = data.data.total
    },
    // 改变时会触发每页条数
    handleSizeChange (newSize) {
      this.queryInfo.pagesize = newSize
      this.getCateList()
    },
    // 改变时会触发当前页
    handleCurrentChange (newNum) {
      this.queryInfo.pagenum = newNum
      this.getCateList()
    },
    //  点击添加分类按钮显示对话框
    showCateDialogVisible () {
      // 打开对话框之前获取父级分类列表
      this.getParentCateList()
      this.cateDialogVisible = true
    },
    // 获取父级分类列表数据
    async getParentCateList () {
      const { data } = await this.$axios.get('categories', { params: { type: 2 } })
      if (data.meta.status !== 200) {
        this.cateDialogVisible = false
        return this.$message.error(data.meta.msg)
      }
      this.parentCateList = data.data
    },
    // 级联选择框选择事件
    cascaderHandleChange () {
      // 如果selectKeys长度大于0代表有选中
      if (this.selectKeys.length > 0) {
        this.addCateForm.cat_pid = this.selectKeys[this.selectKeys.length - 1]
        this.addCateForm.cat_level = this.selectKeys.length
      } else {
        this.addCateForm.cat_pid = 0
        this.addCateForm.cat_level = 0
      }
    },
    // 添加分类
    addCate () {
      this.$refs.addCateFormRef.validate(async bool => {
        if (!bool) return this.$message.error('表单预验证失败')
        const { data } = await this.$axios.post('categories', this.addCateForm)
        if (data.meta.status !== 201) return this.$message.error(data.meta.msg)
        this.$message.success(data.meta.msg)
        this.getCateList()
        this.cateDialogVisible = false
      })
    },
    // 监听添加分类对话框关闭事件重置表单数据
    addCateDialogClosed () {
      this.$refs.addCateFormRef.resetFields()
      this.selectKeys = []
      this.addCateForm.cat_level = 0
      this.addCateForm.cat_pid = 0
    }
  }
}
</script>

<style scoped>
  .margin-button{
    margin-bottom: 15px;
  }
  .el-cascader{
    width: 100%;
  }
  .treetable{
    min-width: 800px;
  }
</style>
